
package de.swta.freezay.database.wicketAdapter;

import de.swta.freezay.database.JPAController;
import de.swta.freezay.database.dbEntities.Item;
import org.apache.wicket.model.LoadableDetachableModel;

/**
 * A LoadableDetachableModel for item entities
 *
 * @author Christian Baeumer
 * @see LoadableDetachableModel
 */
public class DetachableItemModel extends LoadableDetachableModel<Item> {

    /** the item id f this item model */
    private int itemID;

    public DetachableItemModel(final Item i) {
        this(i.getId());
    }


    public DetachableItemModel(final int id) {
        itemID = id;
    }

    @Override
    protected Item load() {
        /** loads item from database */
        return JPAController.ItemJpaController.findItem(itemID);
    }



    @Override
    public int hashCode() {
        int hash = 3;
        hash = 47 * hash + itemID;
        return hash;
    }

    /**
     * used for dataview with ReuseIfModelsEqualStrategy item reuse strategy
     *
     * @see org.apache.wicket.markup.repeater.ReuseIfModelsEqualStrategy
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(final Object obj)
    {
        if (obj == null) {
            return false;
        } else if (obj == this) {
            return true;
        } else if (obj instanceof DetachableItemModel) {
            DetachableItemModel other = (DetachableItemModel) obj;
            return other.itemID == itemID;
        }

        return false;
    }

}
